// Largest prime factor
// Problem 3
// The prime factors of 13195 are 5, 7, 13 and 29.
// What is the largest prime factor of the number 600851475143 ?

var start = new Date().getTime();

var number = 600851475143;

var isPrime = function (n) {
	 if ( isNaN(n) || !isFinite(n) || n % 1 || n < 2) {
		return false;
	 }; 
	 
	 if (n % 2 == 0) {
		return ( n == 2);
	 }
	 
	 var m = Math.sqrt(n);
	 
	 for (var i=3; i <= m; i += 2) {
		if (n % i == 0) 
		{
			return false;
		}
	 }
	 
	 return true;
}


for(var i = 2; i < number; i++)
{
	if(number % i == 0 && isPrime(i))
	{
		number = number / i;		
	}
}

console.log(number);

var end = new Date().getTime();
var time = end - start;

console.log("Time: ", time);